import os

from config import conf

cwd = __file__


def __chdir_to_cur():
    old_dir = os.getcwd()
    os.chdir(os.path.dirname(__file__))
    return old_dir


def __get_id2label():
    old_dir = __chdir_to_cur()
    result = eval(open(conf.id2label_path, 'r', encoding='utf-8').read())
    print(f'load id2label:{result}')
    os.chdir(old_dir)
    return result


def __get_label2id():
    old_dir = __chdir_to_cur()
    result = eval(open(conf.label2id_path, 'r', encoding='utf-8').read())
    print(f'load label2id:{result}')
    os.chdir(old_dir)
    return result


def __get_stopwords():
    old_dir = __chdir_to_cur()
    result = {line.strip() for line in
              open(conf.stopwords_path, 'r', encoding='utf-8').readlines()}
    os.chdir(old_dir)
    return result


def ensure_pdir_exist(path):
    parent_dir = os.path.dirname(path)
    if not os.path.exists(parent_dir):
        os.makedirs(parent_dir, exist_ok=True)


id2label = __get_id2label()
label2id = __get_label2id()
stopwords = __get_stopwords()


def _test_ensure():
    path = './data/rf2/test.txt'
    ensure_pdir_exist(path)


def _test():
    print(id2label)
    print(label2id)
    print(stopwords)


if __name__ == '__main__':
    # _test_ensure()
    _test()
